package org.axed.user.client;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A CaretWidget can receive a soft Caret(Base).
 */
public class CaretWidget extends Widget {
	/**
	 * The axed div is the outermost div. 
	 */
	protected Element outDiv;
	
	/**
	 * The scroll div is for having the contents scrollable.
	 */
	protected Element scrollDiv;
	
	public static final int borderLeft = 3;
	public static final int borderRight = 3;

	public int setWidth;
	public int setHeight = 400;

	/**	
	 * Constructor.
	 */
	public CaretWidget()  {
		super();
	}

	/**
	 * Return the coordinates of the cursor
	 */
	public void getCursorCoords(Coords coords) {
		// nothing, has to be extended
	}

	/**
	 * Returns the absolute right of the widget, until the next anchor.
	 */
	public int getAbsoluteBottom() {
		return DOM.getAbsoluteTop(outDiv) + DOM.getElementPropertyInt(outDiv, "offsetHeight");
	}

	/**
	 * Returns the absolute left of the widget, until the next anchor.
	 */
	public int getAbsoluteLeft() {
		return DOM.getAbsoluteLeft(outDiv);
	}

	/**
	 * Returns the absolute bottom of the inner widget (that is without scrollbar)
	 */
	public int getAbsoluteInnerBottom() {
		return DOM.getAbsoluteTop(scrollDiv) + DOM.getElementPropertyInt(scrollDiv, "clientHeight");
	}

	/**
	 * Returns the absolute right of the inner widget (that is without scrollbar)
	 */
	public int getAbsoluteInnerRight() {
		return DOM.getAbsoluteLeft(scrollDiv) + DOM.getElementPropertyInt(scrollDiv, "clientWidth");
	}

	/**
	 * Returns the absolute right of the widget.
	 */
	public int getAbsoluteRight() {
		return DOM.getAbsoluteLeft(outDiv) + DOM.getElementPropertyInt(outDiv, "offsetWidth");
	}

	/**
	 * Returns the absolute top of the widget.
	 */
	public int getAbsoluteTop() {
		return DOM.getAbsoluteTop(outDiv);
	}

	/**
	 * Returns the heightwidth of the widget excluding scrollbars.
	 */
	public int getClientHeight() {
		return DOM.getElementPropertyInt(scrollDiv, "clientHeight");
	}

	/**
	 * Returns the width of the widget excluding scrollbars.
	 */
	public int getClientWidth() {
		return DOM.getElementPropertyInt(scrollDiv, "clientWidth");
	}

	/**
	 * Returns the height of the widhget.
	 */
	public int getHeight() {
		return DOM.getElementPropertyInt(outDiv, "offsetHeight");
	}

	/**
	 * Returns the bottom edge of the widget.
	 */
	public int getOffsetBottom() {
		return DOM.getElementPropertyInt(outDiv, "offsetTop") + DOM.getElementPropertyInt(outDiv, "offsetHeight");
	}

	/**
	 * Returns the left edge of the widget.
	 */
	public int getOffsetLeft() {
		return DOM.getElementPropertyInt(outDiv, "offsetLeft");
	}

	/**
	 * Returns the right edge of the widget.
	 */
	public int getOffsetRight() {
		return DOM.getElementPropertyInt(outDiv, "offsetLeft") + DOM.getElementPropertyInt(outDiv, "offsetWidth");
	}

	/**
	 * Returns the top edge of the widget.
	 */
	public int getOffsetTop() {
		return DOM.getElementPropertyInt(outDiv, "offsetTop");
	}

	/**
	 * Returns the scroll div element.
	 */
	public Element getScrollDiv() {
		return scrollDiv;
	}

	/**
	 * Returns the vertical scroll position of the edit area bottom.
	 */
	public int getScrollBottom() {
		return DOM.getElementPropertyInt(scrollDiv, "scrollTop") + 
		       DOM.getElementPropertyInt(scrollDiv, "clientHeight");
	}

	/**
	 * Returns the horizontal scroll position of the edit area right end.
	 */
	public int getScrollRight() {
		return DOM.getElementPropertyInt(scrollDiv, "scrollLeft") + 
		       DOM.getElementPropertyInt(scrollDiv, "clientWidth");
	}

	/**
	 * Returns the horizontal scroll position.
	 */
	public int getScrollLeft() {
		return DOM.getElementPropertyInt(scrollDiv, "scrollLeft");
	}


	/**
	 * Returns the vertical scroll position.
	 */
	public int getScrollTop() {
		return DOM.getElementPropertyInt(scrollDiv, "scrollTop");
	}

	/**
	 * Returns the width of the widget.
	 */
	public int getWidth() {
		return DOM.getElementPropertyInt(outDiv, "offsetWidth");
	}

	/**
	 * Sets the height of the widget.
	 */
	public void setHeight(int height) {
		DOM.setStyleAttribute(outDiv,    "height", height + "px");
		DOM.setStyleAttribute(scrollDiv, "height", height + "px");
	}

	/**
	 * Sets the vertical scrollposition so that the bottom  of the 
	 * viewbox is at scrollBottom.
	 *
	 * @param scrollBottom buttom to scroll the bottom of the viewbox to.
	 */
	public void setScrollBottom(int scrollBottom) {
		DOM.setElementPropertyInt(scrollDiv, "scrollTop", 
		    scrollBottom - DOM.getElementPropertyInt(scrollDiv, "clientHeight"));
	}

	/**
	 * Sets the horizonal scrollposition.
	 *
	 * @param scrollLeft the position to scroll to.
	 */
	public void setScrollLeft(int scrollLeft) {
		DOM.setElementPropertyInt(scrollDiv, "scrollLeft", scrollLeft);
	}

	/**
	 * Sets the horizontal scrollposition so that the right end 
	 * of the viewbox is at scrollRight.
	 *
	 * @param scrollRight the position to scroll to.
	 */
	public void setScrollRight(int scrollRight) {
		DOM.setElementPropertyInt(scrollDiv, "scrollLeft", 
		                          scrollRight - DOM.getElementPropertyInt(scrollDiv, "clientWidth"));
	}

	/**
	 * Sets the vertical scrollposition.
	 *
	 * @param scrollTop the position to scroll to.
	 */
	public void setScrollTop(int scrollTop) {
		DOM.setElementPropertyInt(scrollDiv, "scrollTop", scrollTop);
	}

}
